from django.db import models


# Create your models here.
class ParkingLot(models.Model):
    """
        场库基本信息
    """
    Parking_name = models.CharField(u'停车库名称', unique=True, max_length=50, null=True, blank=True)
    Total_num = models.IntegerField(u'总车位数', default=1, blank=True)
    # Surplus_num = models.IntegerField(u'剩余车位数', default=1, blank=True)         #这个应该是计算出来的
    Admin = models.ForeignKey('auth.User', verbose_name='admin', null=True, blank=True)
    Interim_num = models.IntegerField(u'临时车位数', default=1, blank=True)
    Work_time_st = models.DateTimeField(u'工作开始时间', null=True)
    Work_time_ed = models.DateTimeField(u'工作结束时间', null=True)

    def __str__(self):
        return '%s  --  %s' % (self.Parking_name, self.Total_num)

    class Meta:
        verbose_name = '停车场基本信息'
        verbose_name_plural = '停车场基本信息表'


class ClientList(models.Model):
    """
    记录客户端的IP地址，也作为出入口数量、安装信息的记录
    """
    Position_name = models.CharField(u'出入口名称', unique=True, max_length=50, null=True, blank=True)  # IP地址唯一
    PRC_IP = models.GenericIPAddressField(u'客户端IP', unique=True, null=True, blank=True)  # IP地址唯一
    Way_num = models.SmallIntegerField(u'通道号', default=1, blank=True)
    Install_position = models.CharField(u'安装位置', max_length=50, null=True, blank=True)

    def __str__(self):
        return '%s  --  %s' % (self.Install_position, self.PRC_IP)

    class Meta:
        verbose_name = '出入口客户端'
        verbose_name_plural = '出入口客户端配置表'


class CameraPosition(models.Model):
    """
    出入口摄像头安装的
    """

    Position = models.ForeignKey('manager.ClientList', verbose_name='出入口信息', null=True, blank=True)
    camera_IP = models.GenericIPAddressField(u'摄像头IP', unique=True, null=True, blank=True)  # IP地址唯一
    WAY_TYPE = ((1, '出库'), (0, '入库'))
    Direction = models.SmallIntegerField(u'方向', choices=WAY_TYPE, null=True, blank=True)

    def __str__(self):
        return '%s  --  %s  --  %s' % (self.Position.Position_name, self.Camera_IP, self.Direction)

    class Meta:
        verbose_name = '摄像头'
        verbose_name_plural = '摄像头配置表'


class Expense(models.Model):
    """
    计费公式
    """
    Free_time = models.FloatField(u'免费时长', default=0.0, null=True, blank=True)

    Basic = models.FloatField(u'起步价', default=0.0, null=True, blank=True)
    Basic_time = models.FloatField(u'起步时长', default=0.0, null=True, blank=True)

    Period_one = models.FloatField(u'第一阶梯价', default=0.0, null=True, blank=True)
    Period_one_time = models.FloatField(u'第一阶梯时长', default=0.0, null=True, blank=True)

    Period_two = models.FloatField(u'第二阶梯价', default=0.0, null=True, blank=True)
    Period_two_time = models.FloatField(u'第二阶梯时长', default=0.0, null=True, blank=True)

    Max_day = models.FloatField(u'当日最高价', default=0.0, null=True, blank=True)

    Leisure = models.FloatField(u'闲时价格', default=0.0, null=True, blank=True)
    Leisure_st = models.DateTimeField(u'闲时开始时间', null=True)
    Leisure_en = models.DateTimeField(u'闲时结束时间', null=True)
    Is_active = models.BooleanField(u'计费模式有效', default=1)

    def __str__(self):
        return '基础价：%s元,   每日最高收费：%s元' % (self.Basic, self.Max_day)

    class Meta:
        verbose_name = '计费'
        verbose_name_plural = '计费方式表'


class EventLog(models.Model):
    """
    车辆流水及收费明细,展示用
    """
    card_num = models.CharField(u'车牌号', max_length=50, null=True, blank=True)
    CAR_TYPE = ((0, '月租'), (1, '临时车'), (2, '特殊车辆'))
    car_type = models.IntegerField(u'车辆类型', choices=CAR_TYPE, null=True)
    in_pic = models.CharField(u'进库图片地址', max_length=100, null=True, blank=True)
    in_timer = models.DateTimeField(u'进库时间', null=True)
    out_pic = models.CharField(u'出库图片地址', max_length=100, null=True, blank=True)
    out_timer = models.DateTimeField(u'出库时间', null=True)
    counteract = models.BooleanField(u'入出抵消', default=0, blank=True)  # 为0则只进入了，为1则有进出记录
    # 0：自动识别，1 ：手动识别
    avtomatik = models.SmallIntegerField(u'自动识别', default=0, blank=True)
    free = models.FloatField(u'优惠抵扣金额', default=0, blank=True)
    payable = models.FloatField(u'应付', default=0.0)
    actuallypay = models.FloatField(u'实付', default=0.0)
    # wuxm add 增加记录入库和更新时间
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)
    def __str__(self):
        return '%s  --  %s  --  %s  --  %s' % (self.card_num, self.in_timer,
                                               self.out_timer, self.actuallypay)
    class Meta:
        verbose_name = '收费明细'
        verbose_name_plural = '收费明细表'